import { defineStore } from 'pinia'
import request from '@/utils/request'

export const useUserStore = defineStore('user', {
  state: () => ({
      token: '',
      userId: '',
      nickname: '未登录',
      avatar: '/public/images/avatar.png',
      userNo: '0',
  }),
  actions: {
    isLogin() {
      return this.token !== ''
    },
    async getUserInfo() {
      const res = await request.get('/user/info')
      this.token = res.data.token
      this.userId = res.data.userId
      this.nickname = res.data.nickname
      this.avatar = res.data.avatar
      this.userNo = res.data.userNo
    },
    // 更新用户信息
    // async updateUserInfo(data: any) {
    //     const res = await request.post('/user/update', data)
    //     this.userInfo = res
    // },
    setUserInfo(data: any) {
      console.log('data:',data)
      this.token = data.token
      this.userId = data.userId
      this.nickname = data.nickname
      this.avatar = data.avatar
      this.userNo = data.userNo
    },
    logout() {
        this.token = ''
        this.userId = ''
        this.nickname = ''
        this.avatar = ''
        this.userNo = ''
    }
    
    
  },
  persist: true // 一行搞定持久化
})